package com.movie.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.datacomm.common.CommResp;
import com.movie.datacomm.model.Admin;
import com.movie.datacomm.model.User;
import com.movie.datacomm.service.AdminService;
import com.movie.datacomm.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    AdminService adminService;

    @Autowired
    UserService userService;

    @RequestMapping(value = "/login", method = RequestMethod.POST)
    @ResponseBody
    public CommResp login(@RequestBody Admin obj){

        if(null == obj || StringUtils.isEmpty(obj.getName()) || StringUtils.isEmpty(obj.getPassword())){
            return CommResp.fail("用户名或密码不能为空");
        }
        Admin admin = adminService.getOne(new QueryWrapper<Admin>().eq("name", obj.getName()));
        if(null == admin || !obj.getPassword().trim().equalsIgnoreCase(admin.getPassword())){
            return CommResp.fail("用户名或密码不正确");
        }
        return CommResp.ok(admin);
    }

    @RequestMapping( "/getAdminInfo")
    @ResponseBody
    public Object getAdminInfo(int adminId){

        System.out.println("getAdminInfo");

        Admin admin = adminService.getById(adminId);
        if(null != admin){
            return CommResp.ok(admin);
        }else{
            return CommResp.fail("");
        }
    }

    @RequestMapping(value = "/getCurrentPageUser", method = RequestMethod.GET)
    @ResponseBody
    public CommResp getCurrentPageUser(int currentPage, int pageSize, @Nullable String input){

        Page<User> page = new Page<>(currentPage, pageSize);

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(input)){
            wrapper.like("user_name", input);
        }
        wrapper.orderByAsc("user_id");

        page = userService.page(page, wrapper);
        return CommResp.okList(page.getRecords(), page.getTotal());
    }

    @RequestMapping(value = "/updateUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp updateUserInfo(@RequestBody User user){
        User tmpUser = userService.getById(user.getUserId());
        if(null == tmpUser){
            return CommResp.fail("用户不存在");
        }

        boolean result = userService.updateById(user);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("更新失败");
        }
    }

    @RequestMapping(value = "/deleteUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp deleteUserInfo(@RequestBody User user){

        boolean result = userService.removeById(user.getUserId());
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("删除失败");
        }
    }

    @RequestMapping(value = "/addUserInfo", method = RequestMethod.POST)
    @ResponseBody
    public CommResp addUserInfo(@RequestBody User user){

        if(StringUtils.isEmpty(user.getAvatar())){
            user.setAvatar("/images/avatar/monkey.png");
        }

        int count = userService.count(new QueryWrapper<User>().eq("user_name", user.getUserName()).or().eq("phone", user.getPhone()));
        if(count > 0){
            return CommResp.fail("用户名或手机号已存在");
        }
        boolean result = userService.save(user);
        if(result) {
            return CommResp.ok();
        }else{
            return CommResp.fail("操作失败");
        }
    }

}
